import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/OrderView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/RegisterView.vue')
  },
  {
    path: '/shopcart',
    name: 'shopcart',
    component: () => import('../views/ShopcartView.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/DetailsView.vue')
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from) => {
  if (to.path != '/login' && to.path != '/' && to.path != "/register" && to.path !='/details') {
    console.log(store.state.userInfo.username);
    if (store.state.userInfo.username == "") {
      return {
        path: '/login',
      };
    }
  }

})

export default router
